package com.yztc.xiami_app.adapter.homepage;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.xiami_app.R;
import com.yztc.xiami_app.bean.goods.BaseBean;
import com.yztc.xiami_app.bean.goods.GoodsBean;
import com.yztc.xiami_app.ui.goods.interfaces.IGoodsItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周旭 on 2016/11/16.
 */

//首页中商品列表的适配器
public class GoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_AD = 1; //广告的类型
    private static final int TYPE_GOODS = 2; //商品的类型
    private static final int[] IMAGE_AD = {R.mipmap.ad1, R.mipmap.ad2, R.mipmap.ad3, R.mipmap.ad4, R.mipmap.ad5};

    //---------轮播的广告-------------
    private List<ImageView> mAdList = new ArrayList<>(); //广告轮播的数据源
    private ADViewHolder.ADPlayRunnable adPlayRunnable;
    private Handler handler = new Handler();

    //----------other---------------
    private List<BaseBean> mGoodsList;
    private LayoutInflater inflater;
    private Context context;
    private IGoodsItemClickListener listener; //商品对象被点击的监听器


    public GoodsAdapter(Context context, List<BaseBean> mGoodsList,IGoodsItemClickListener listener) {
        this.mGoodsList = mGoodsList;
        this.context = context;
        this.listener = listener;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getItemViewType(int position) {
        //获取Bean类型
        int type = mGoodsList.get(position).getBean_type();
        switch (type) {
            case 1:
                return TYPE_AD;
            case 2:
                return TYPE_GOODS;
            default:
                return 0;
        }
    }

    @Override
    public int getItemCount() {
        return mGoodsList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        switch (viewType) {
            case TYPE_GOODS:
                itemView = inflater.inflate(R.layout.home_page_item_goods, parent, false);
                return new GoodsViewHolder(itemView);
            case TYPE_AD:
                itemView = inflater.inflate(R.layout.home_page_ad, parent, false);
                return new ADViewHolder(itemView);
            default:
                return null;
        }
    }

    //绑定
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ADViewHolder) {
            //Nothing

        } else if (holder instanceof GoodsViewHolder) {
            final GoodsBean.TopickusBean.ItemsBean itemsBean = (GoodsBean.TopickusBean.ItemsBean) mGoodsList.get(position);
            //设置视图相关数据
            String name = itemsBean.getSkuLinkName();
            if(name.length() > 7){
                name = name.substring(0,7);
            }
            ((GoodsViewHolder) holder).goodsTitle.setText(name); //设置商品名称
            ((GoodsViewHolder) holder).goodsPrice.setText("¥ "+itemsBean.getPrice()); //设置商品价格
            //设置商品图片
            Glide.with(context).load(itemsBean.getSkuImgurl())
                    .error(R.drawable.mac) //加载错误
                    .into(((GoodsViewHolder) holder).goodsIconIv);



            //设置商品的点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //通知首页Fragment来处理商品被点击的点击事件
                    //获取产品的ID，用于传递给商品详情页面
                     int productId = itemsBean.getProductId();
                    listener.onGoodsItemClick(productId);
                }
            });
        }
    }

    //初始化广告的数据源
    private void initADData() {
        //动态生成数据源
        for (int i = 0; i < IMAGE_AD.length; i++) {
            ImageView iv = new ImageView(context);
            iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); //设置布局
            iv.setScaleType(ImageView.ScaleType.FIT_XY);//设置拉伸方式
            iv.setImageResource(IMAGE_AD[i]); //设置图片内容
            mAdList.add(iv); //把ImageView添加到数据源
        }
    }

    //设置广告轮播的ViewPager
    private void initAdViewPager(ViewPager adVp) {
        ADAdapter adapter = new ADAdapter(mAdList);
        //广告轮播的ViewPager视图加载适配器
        adVp.setAdapter(adapter);
        adVp.setOffscreenPageLimit(mAdList.size() - 1);
    }



    //显示当前广告
    private void showCurrAd(int position,LinearLayout adDotLayout) {

        for (int i = 0; i < adDotLayout.getChildCount(); i++) {
            View view = adDotLayout.getChildAt(i);
            if (i == position) {
                //如果是当前显示的图片对应的小圆点
                view.setSelected(true); //设置高亮显示
            } else {
                view.setSelected(false); //设置普通显示
            }
        }
    }

    //轮播的广告，布局类型为1
    class ADViewHolder extends RecyclerView.ViewHolder {

        private ViewPager adVp;
        private LinearLayout adDotLayout;

        public ADViewHolder(View itemView) {
            super(itemView);
            adVp = (ViewPager) itemView.findViewById(R.id.vp_ad);
            adDotLayout = (LinearLayout) itemView.findViewById(R.id.layout_ad_dot);

            initADData();
            initAdViewPager(adVp);
            //setADPlay();
            //启动延时的任务
            adPlayRunnable = new ADPlayRunnable();
            handler.postDelayed(adPlayRunnable, 2000);
        }


        //广告条轮播的异步任务
        class ADPlayRunnable implements Runnable {
            @Override
            public void run() {

                int currImg = adVp.getCurrentItem(); //获取当前图片的位置
                currImg++;
                if (currImg > 3) {
                    currImg = 0; //从第一张图片播放
                }
                adVp.setCurrentItem(currImg); //设置当前广告图片
                showCurrAd(currImg,adDotLayout);
                //递归调用自身实现轮播
                handler.postDelayed(adPlayRunnable, 2000);
            }
        }
    }

    //商品，布局类型为2
    class GoodsViewHolder extends RecyclerView.ViewHolder {

        private ImageView goodsIconIv; //商品图标
        private TextView goodsTitle; //商品名称
        private TextView goodsPrice; //商品价格

        public GoodsViewHolder(View itemView) {
            super(itemView);
            goodsIconIv = (ImageView) itemView.findViewById(R.id.iv_goods_icon);
            goodsTitle = (TextView) itemView.findViewById(R.id.tv_goods_title);
            goodsPrice = (TextView) itemView.findViewById(R.id.tv_goods_price);
        }
    }
}
